﻿using System.Linq;
using DevExpress.XtraBars.Ribbon;

namespace WinForm.Common
{
    /// <summary>
    /// 辅助完成用户控件对应的RibbonPage及其中的按钮事件等的管理
    /// </summary>
    public class RibbonMenuController
    {

        #region  属性

        public MainForm Parent { get; private set; }

        #endregion
        
        public RibbonMenuController(MainForm parent)
        {
            Parent = parent;
        }

        /// <summary>
        /// 初始化RibbonContron,即：隐藏所有功能模块对应的RibbonPage，只留下tag为null的
        /// </summary>
        public void InitRibbonMenu()
        {
            RibbonControl ribbonControl = Parent.RibbonMenu;

            //隐藏 RibbonPage
            foreach (RibbonPage page in ribbonControl.TotalPageCategory.Pages.Cast<RibbonPage>().Where(page => page.Tag != null))
            {
                page.Visible = false;
            }

            //隐藏 RibbonPageCategory

            ribbonControl.PageCategories[0].Visible = false;
            //foreach (RibbonPageCategory pageCategory in ribbonControl.PageCategories)
            //{
            //    pageCategory.Visible = false;
            //}

        }

        /// <summary>
        /// 显示tag值为pageTag的page
        /// </summary>
        public void ShowRibbonPageForCurrentModule()
        {
            //if (Parent.CurrentModule==null) return;

            RibbonControl ribbonControl = Parent.RibbonMenu;
            string pageTag = Parent.CurrentModule.TypeName;

            if (pageTag == null) return;

            foreach (RibbonPage page in ribbonControl.TotalPageCategory.Pages.Cast<RibbonPage>().Where(page => page.Tag != null))
            {
                if (page.Tag.Equals(pageTag))
                {
                    page.Visible = true;
                    page.Text = ResourcesHelper.GetString("DefaultPageName");
                    ribbonControl.SelectedPage = page;
                }
                else
                {
                    page.Visible = false;
                }
            }
        }

        /// <summary>
        /// 显示Name值为pageName的CategoriePage
        /// </summary>
        public void ShowRibbonPageForActiveDetail()
        {
            RibbonControl ribbonControl = Parent.RibbonMenu;
            if (Parent.ActiveDetailControl == null)
            {
                ribbonControl.PageCategories[0].Visible = false;
                return;
            }

            
            string pageTag = Parent.ActiveDetailControl.GetType().Name;
            if (string.IsNullOrEmpty(pageTag)) return;

            foreach (RibbonPage page in ribbonControl.PageCategories[0].Pages)
            {
                if (page.Tag.Equals(pageTag))
                {
                    ribbonControl.PageCategories[0].Visible = true;
                    page.Visible = true;
                    ribbonControl.SelectedPage = page;
                }
                else
                {
                    page.Visible = false;
                }
            }
        }

        /// <summary>
        /// 为一些按钮注册单击事件
        /// </summary>
        public void InitButtonActions()
        {
            //Parent.AddButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.Add(); });
            //Parent.EditButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.Edit(); });
            Parent.DeleteButton.ItemClick += delegate { Parent.CurrentModuleControl.Delete(); };
            Parent.RefreshButton.ItemClick += delegate { Parent.CurrentModuleControl.RefreshData(); };
            Parent.FirstButton.ItemClick += delegate { Parent.CurrentModuleControl.First(); };
            Parent.PrevButton.ItemClick += delegate { Parent.CurrentModuleControl.Previous(); };
            Parent.NextButton.ItemClick += delegate { Parent.CurrentModuleControl.Next(); };
            Parent.LastButton.ItemClick += delegate { Parent.CurrentModuleControl.Last(); };

            Parent.SaveButton.ItemClick += delegate { Parent.CurrentModuleControl.ActiveDetailControl.Save(); };
            Parent.SaveAndCloseButton.ItemClick += delegate { Parent.CurrentModuleControl.ActiveDetailControl.SaveAndClose(); };
            Parent.CloseButton.ItemClick += delegate { Parent.CurrentModuleControl.ActiveDetailControl.Close(); };





            Parent.LargeIconButton.ItemClick += delegate { Parent.CurrentModuleControl.LargeIconView(); };
            Parent.SmallIconButton.ItemClick += delegate { Parent.CurrentModuleControl.SmallconView(); };
            Parent.ListButton.ItemClick += delegate { Parent.CurrentModuleControl.ListView(); };
            Parent.DetailsButton.ItemClick += delegate { Parent.CurrentModuleControl.DetailsView(); };
            Parent.TileButton.ItemClick += delegate { Parent.CurrentModuleControl.TileView(); };


            //Parent.OptionsButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.EditOptions(); });
            //Parent.CurrentCustomerButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.SetCurrentCustomer(); });
            //Parent.ChangeCustomerButton.ItemClick += new ItemClickEventHandler(delegate { Parent.LayoutManager.ShowFindCustomerForm(Parent, Parent.MenuManager); });
            //Parent.RentButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.RentMovie(); });
            //Parent.ReturnButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ReturnMovie(); });
            //Parent.ActiveRentButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.CheckActiveItems(); });
            //Parent.SaveButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.DetailSave(); });
            //Parent.SaveAndCloseButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.DetailSaveAndClose(); });
            //Parent.CloseButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.DetailClose(); });
            //Parent.LoadPictureButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.LoadPicture(); });
            //Parent.ClearPictureButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ClearPicture(); });
            //Parent.MovieAddItemButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.MovieAddItem(); });
            //Parent.MovieManageItemsButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.MovieManageItems(); });
            //Parent.AddPictureButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.AddPicture(); });
            //Parent.DeletePictureButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.DeletePicture(); });
            //Parent.ViewStylesMenu.Popup += new EventHandler(ViewStylesMenu_Popup);
            //Parent.MainViewButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.DoViewChange(true); });
            //Parent.AlternateViewButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.DoViewChange(false); });
            //Parent.CloseDetailsButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.CloseAllDetails(); });
            //Parent.MovieCategoriesButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ShowMovieCategories(); });

            //Parent.HomeButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ShowMasterModule(); });
            //Parent.PrintPreviewButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.PrintPreview(); });
            //Parent.ExportToPDFButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ExportToPDF(); });
            //Parent.ExportToXMLButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ExportToXML(); });
            //Parent.ExportToHTMLButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ExportToHTML(); });
            //Parent.ExportToMHTButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ExportToMHT(); });
            //Parent.ExportToXLSButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ExportToXLS(); });
            //Parent.ExportToXLSXButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ExportToXLSX(); });
            //Parent.ExportToRTFButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ExportToRTF(); });
            //Parent.ExportToImageButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ExportToImage(); });
            //Parent.ExportToTextButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.ExportToText(); });
            //Parent.RotateLayoutButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.RotateLayout(); });
            //Parent.FlipLayoutButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.FlipLayout(); });
            //Parent.SaveCurrentRecordButton.ItemClick += new ItemClickEventHandler(delegate { Parent.CurrentModuleControl.SaveCurrentRecord(); });
        }

        ///// <summary>
        ///// 为当前模块控件创建细节控件菜单
        ///// </summary>
        ///// <param name="ribbonControl"></param>
        ///// <param name="currentModuleControl"></param>
        //internal void CreateDetailRibbon(RibbonControl ribbonControl, ModuleBase currentModuleControl)
        //{
            //foreach (RibbonPage page in ribbonControl.TotalPageCategory.Pages)
            //{
            //    if (page.Tag == null ||
            //        currentModuleControl.ActiveDetailControl == null ||
            //        CurrentControl.ActiveDetailControl.ActiveRibbonPage != null) continue;
            //    if (page.Tag.Equals(CurrentControl.DetailTypeName))
            //        CurrentControl.ActiveDetailControl.CreateActiveRibbonPage(page);
            //}
        //}
        //bool lockPageChanging = false;
        //internal void UpdateMenu() {
        //    lockPageChanging = true;
        //    foreach(RibbonPage page in Ribbon.TotalPageCategory.Pages) {
        //        if(page.Tag == null) continue;
        //        page.Visible = CurrentControl.IsSuitablePage(page);
        //        if(page.Visible && page.Tag.Equals(CurrentControl)) {
        //            parent.RibbonControl.SelectedPage = page;
        //        }
        //    }
        //    lockPageChanging = false;
        //}
        //void InitRibbonMenu() {
        //    UpdateMenu();
        //    parent.AddButton.Hint = string.Format(ConstStrings.Get("AddButtonHint"), CurrentControl.EditObjectName);
        //    parent.EditButton.Hint = string.Format(ConstStrings.Get("EditButtonHint"), CurrentControl.EditObjectName);
        //    parent.DeleteButton.Hint = string.Format(ConstStrings.Get("DeleteButtonHint"), CurrentControl.EditObjectName);
        //    parent.RefreshButton.Hint = CurrentControl.EditObjectName == string.Empty ? ConstStrings.Get("RefreshDefaultButtonHint") : string.Format(ConstStrings.Get("RefreshButtonHint"), CurrentControl.EditObjectName);
        //    parent.PrevButton.Hint = string.Format(ConstStrings.Get("PrevButtonHint"), CurrentControl.EditObjectName);
        //    parent.NextButton.Hint = string.Format(ConstStrings.Get("NextButtonHint"), CurrentControl.EditObjectName);
        //    parent.OptionsButton.Hint = string.Format(ConstStrings.Get("EditViewOptions"), CurrentControl.EditObjectName);
        //    if(!string.IsNullOrEmpty(CurrentControl.EditObjectName) && CurrentControl.UseList) {
        //        parent.PrintPreviewButton.Hint = string.Format(ConstStrings.Get("PrintPreviewButtonHint"), CurrentControl.EditObjectName, ObjectHelper.GetArticleByWord(CurrentControl.EditObjectName));
        //        parent.ExportButton.Hint = string.Format(ConstStrings.Get("ExportButtonHint"), CurrentControl.EditObjectName);
        //        parent.HomeButton.Hint = string.Format(ConstStrings.Get("HomeButtonHint"), CurrentControl.EditObjectName);
        //    }
        //    else {
        //        parent.PrintPreviewButton.Hint = ConstStrings.Get("PrintPreviewButtonHintDefault");
        //        parent.ExportButton.Hint = ConstStrings.Get("ExportButtonHintDefault");
        //        parent.HomeButton.Hint = string.Empty;
        //    }
        //}
        //public RibbonMenuController(frmMain parent) {
        //    this.parent = parent;
        //    InitRibbonElementsImages();
        //    InitButtonActions();
        //    InitStatusBar();
        //    this.parent.RibbonControl.SelectedPageChanging += new RibbonPageChangingEventHandler(RibbonControl_SelectedPageChanging);    
        //}
        //Control activeControl = null;
        //void RibbonControl_SelectedPageChanging(object sender, RibbonPageChangingEventArgs e) {
        //    if(lockPageChanging) return;
        //    Control control = e.Page.Tag as Control;
        //    if(control == null) return;
        //    if(!control.Visible) control.Show();
        //    control.BringToFront();
        //    if(!Object.Equals(control, activeControl))
        //        WinHelper.CloseCustomizationForm(CurrentControl);
        //    activeControl = control;

        //    lockPageChanging = true;
        //    try {
        //        TutorialControlBase tControl = control as TutorialControlBase;
        //        if(tControl != null) tControl.ActiveDetailControl = null;
        //        DetailBase dControl = control as DetailBase;
        //        if(dControl != null) CurrentControl.ActiveDetailControl = dControl;
        //    }
        //    finally {
        //        lockPageChanging = false;
        //    }
        //}
        //public static void SetBarButtonImage(BarItem item, string name) {
        //    item.LargeGlyph = ElementHelper.GetImage(name, ImageSize.Large32);
        //    item.Glyph = ElementHelper.GetImage(name, ImageSize.Small16);
        //}
        //void InitRibbonElementsImages() {
        //    SetBarButtonImage(parent.AddButton, "Add");
        //    SetBarButtonImage(parent.EditButton, "Edit");
        //    SetBarButtonImage(parent.DeleteButton, "Delete");
        //    SetBarButtonImage(parent.PrevButton, "Previous");
        //    SetBarButtonImage(parent.NextButton, "Next");
        //    SetBarButtonImage(parent.OptionsButton, "View");
        //    SetBarButtonImage(parent.CurrentCustomerButton, "Person");
        //    SetBarButtonImage(parent.ChangeCustomerButton, "UserKey");
        //    SetBarButtonImage(parent.RentButton, "Order");
        //    SetBarButtonImage(parent.ActiveRentButton, "ActiveRents");
        //    SetBarButtonImage(parent.ReturnButton, "Return");
        //    SetBarButtonImage(parent.SaveButton, "Save");
        //    SetBarButtonImage(parent.SaveAndCloseButton, "SaveAndClose");
        //    SetBarButtonImage(parent.CloseButton, "Close");
        //    SetBarButtonImage(parent.LoadPictureButton, "Open");
        //    SetBarButtonImage(parent.ClearPictureButton, "Delete");
        //    SetBarButtonImage(parent.AddPictureButton, "New");
        //    SetBarButtonImage(parent.DeletePictureButton, "Delete");
        //    SetBarButtonImage(parent.MovieAddItemButton, "AddItem");
        //    SetBarButtonImage(parent.MovieManageItemsButton, "ManageItems");
        //    SetBarButtonImage(parent.LayoutOptionsButton, "LayoutOptions");
        //    SetBarButtonImage(parent.ViewStylesMenu, "Views");
        //    SetBarButtonImage(parent.CloseDetailsButton, "CloseDetails");
        //    SetBarButtonImage(parent.ChartPaletteButton, "Palette");
        //    SetBarButtonImage(parent.MovieCategoriesButton, "Categories");
        //    SetBarButtonImage(parent.RefreshButton, "Refresh");
        //    SetBarButtonImage(parent.HomeButton, "Home");
        //    SetBarButtonImage(parent.PrintPreviewButton, "Preview");
        //    SetBarButtonImage(parent.ExportButton, "Export");
        //    SetBarButtonImage(parent.ExportToPDFButton, "ExportToPDF");
        //    SetBarButtonImage(parent.ExportToXMLButton, "ExportToXML");
        //    SetBarButtonImage(parent.ExportToHTMLButton, "ExportToHTML");
        //    SetBarButtonImage(parent.ExportToMHTButton, "ExportToMHT");
        //    SetBarButtonImage(parent.ExportToXLSButton, "ExportToExcel");
        //    SetBarButtonImage(parent.ExportToXLSXButton, "ExportToExcel");
        //    SetBarButtonImage(parent.ExportToRTFButton, "ExportToRTF");
        //    SetBarButtonImage(parent.ExportToImageButton, "ExportToIMG");
        //    SetBarButtonImage(parent.ExportToTextButton, "ExportToTXT");
        //    SetBarButtonImage(parent.RotateLayoutButton, "LayoutRotate");
        //    SetBarButtonImage(parent.FlipLayoutButton, "LayoutFlip");
        //    SetBarButtonImage(parent.PeriodButton, "Period");
        //    SetBarButtonImage(parent.ReceiptPeriodButton, "Period");
        //    SetBarButtonImage(parent.SaveCurrentRecordButton, "Save");
        //}


        //void ViewStylesMenu_Popup(object sender, EventArgs e) {
        //    CurrentControl.ViewStylesPopup(sender);
        //}
        //internal void CalcDetailFormItemsEnabling(bool allow, bool first, bool last) {
        //    parent.EditButton.Enabled = parent.DeleteButton.Enabled = allow;
        //    parent.PrevButton.Enabled = allow && !first;
        //    parent.NextButton.Enabled = allow && !last;
        //}
        //internal void SetAllowDelete(bool allow) {
        //    parent.DeleteButton.Enabled = allow;
        //}
        //internal void SetAllowNavigation(bool allowPrev, bool allowNext) {
        //    parent.PrevButton.Enabled = allowPrev;
        //    parent.NextButton.Enabled = allowNext;
        //}
        //internal void CalcCustomerItemsEnabling(bool allow) {
        //    parent.CurrentCustomerButton.Enabled = allow;
        //}
        //internal void CalcCloseAllDetails() {
        //    parent.CloseDetailsButton.Enabled = CurrentControl.IsDetailsExist;
        //}
        //internal void CalcRentItemsEnabling(bool allowRent, bool allowReturn) {
        //    //TODO parent.RentButton.Enabled = allowRent;
        //    parent.RentButton.Enabled = false; //for the future use    
        //    parent.ReturnButton.Enabled = allowReturn;
        //}
        //internal void CalcRentItemsEnablingEx(bool allowCheck) {
        //    parent.ActiveRentButton.Enabled = allowCheck;
        //}
        //void InitStatusBar() {
        //    BarItemLink link = parent.MainStatusBar.ItemLinks.Add(parent.ChangeCustomerButton);
        //    link.Item.Alignment = BarItemLinkAlignment.Right;
        //    link.UserCaption = "";
        //    link.UserDefine = BarLinkUserDefines.Caption;
        //}
        //public void Add()
        //{
        //    CurrentModuleControl.Add();
        //}
    }
}
